#include "yigsynthbutton.h"
#include <QApplication>
#include "graphics/yigcolorkit.h"
#include <QPainter>

YigSynthButton::YigSynthButton(QString name, QWidget *parent) :
    QPushButton(parent)
{
    mName = name;
    setText(mName);
    //setFlat(true);
    //setGeometry(0,0,parent->width()-20, 25);
    //setStyleSheet("background: rgba(50,50,50,150)");
    setObjectName("YigSynthButton");
    setStyleSheet("QWidget#YigSynthButton{ background: #000; color: #ddd; border-radius: 0px; text-align: left; padding-left: 5px}"
                  "QWidget#YigSynthButton::hover{ background: #bbb; color: #000; }");
    setVisible(true);
}

QString YigSynthButton::getName()
{
    return mName;
}

void YigSynthButton::mousePressEvent(QMouseEvent *e)
{
    emit pressed(mName);

    if(e->buttons() & Qt::LeftButton)
    {
        startPos = e->pos();
    }

    QPushButton::mousePressEvent(e);
}

void YigSynthButton::mouseMoveEvent(QMouseEvent *e)
{
    if(e->buttons() & Qt::LeftButton)
    {
        int distance = (e->pos() - startPos).manhattanLength();
        if(distance >= QApplication::startDragDistance())
        {
            performDrag();
        }
    }
    QPushButton::mouseMoveEvent(e);
}

void YigSynthButton::performDrag()
{
    QMimeData* mimeData = new QMimeData;
    mimeData->setText(mName);
    QDrag* drag = new QDrag(this);
    drag->setMimeData(mimeData);
    drag->exec(Qt::CopyAction);
}
